function random(min, max) {
  return parseInt(Math.random() * (max - min + 1) + min);
}
// 生成范围内的随机数

function selectYin(type, yinObj) {
  if (type == 'q') {
    let newPing = yinObj[type].ping.filter((v, i) => {
      return v.special != true
    })
    let newPian = yinObj[type].pian.filter((v, i) => {
      return v.special != true
    })
    return {
      ping: newPing,
      pian: newPian
    }
  } //清音处理掉特殊的几项
  return yinObj[type]
  // 其他照常输出
}
// 获取对应类型平片假名(对象)

// let title = ['根据平假名选出对应的片假名', '根据片假名选出对应的平假名', '根据提示音选出对应平片假名', '根据平片假名选出对应声音','根据罗马音选出对应的平片假名']

let title = ['根据平假名选出对应的片假名','根据提示音选出对应片假名','根据平假名选出对应声音','根据罗马音选出对应片假名',
'根据片假名选出对应平假名','根据提示音选出对应平假名','根据片假名选出对应声音','根据罗马音选出对应平假名']
// 所有题目
// 选完题目后 根据题目 调用不同的函数  题目分为两大类   前4项为1类  后4项为1类
// 
let titleType = ['normal', 'voiceFind', 'findVoice','pronunciationFind']



/**
 * yin:清,浊,拗对象 包括 平片假名
 * pingIndex:随机生成的平假名下标
 */
function pingFindpian(yin, pingIndex) {
  let pian = yin.pian
  let answer = {
    index: pingIndex,
    text: pian[pingIndex].text,
    pronunciation: pian[pingIndex].pronunciation,
    real: true
  } // 答案对象
  let allAnswerObj = {}
  // 过滤重复
  let allAnswerArr = [answer]
  // 存放所有答案对象
  allAnswerObj[pingIndex] = 1;
  while(allAnswerArr.length < 4){
    let i = random(0,pian.length -1)
    if(allAnswerObj[i] == undefined){
      allAnswerObj[i] = 1
      allAnswerArr.push({
        index: i,
        text: pian[i].text,
        pronunciation: pian[i].pronunciation,
        real: false
      })
    }
  }
  let allAnswer = allAnswerArr.sort( (a,b) => {
    return (Math.random() - 0.5)
  })
  return allAnswer
}
// 随机找出 平假名 (special值为true的不要) 
// 根据  平假名的下标 找到 片假名 答案
// 随机找出三个 片假名 (special值true的不要) 不能与答案一样

function pianFindping(yin,pianIndex){
  let ping = yin.ping
  let answer = {
    index: pianIndex,
    text: ping[pianIndex].text,
    pronunciation: ping[pianIndex].pronunciation,
    real: true
  } 
  let allAnswerObj = {}
  let allAnswerArr = [answer]
  allAnswerObj[pianIndex] = 1;
  while (allAnswerArr.length < 4) {
    let i = random(0, ping.length - 1)
    if (allAnswerObj[i] == undefined) {
      allAnswerObj[i] = 1
      allAnswerArr.push({
        index: i,
        text: ping[i].text,
        pronunciation: ping[i].pronunciation,
        real: false
      })
    }
  }
  let allAnswer = allAnswerArr.sort((a, b) => {
    return (Math.random() - 0.5)
  })
  return allAnswer
}


module.exports = {
  random,
  selectYin,
  pingFindpian,
  pianFindping,
  title,
  titleType
}